package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-05-13 15:39
 */
@Controller
public class ActivityController {

    @Autowired
    private ActivityFeignClient activityFeignClient;


    /**
     * 秒杀列表页渲染
     *
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String seckillHtml(Model model) {
        Result result = activityFeignClient.getSeckillGoodsList();
        model.addAttribute("list", result.getData());
        return "/seckill/index";
    }


    /**
     * 秒杀商品详情页渲染
     *
     * @param model
     * @param skuId
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String seckillGoodsItemHtml(Model model, @PathVariable("skuId") Long skuId) {
        Result result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item", result.getData());
        return "/seckill/item";
    }


    /**
     * 秒杀前排队页面
     *
     * @param skuId   商品ID
     * @param buyCode 抢购码
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String seckillQueueHtml(Model model, @RequestParam("skuId") Long skuId, @RequestParam("skuIdStr") String buyCode) {
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", buyCode);
        return "/seckill/queue";
    }


    /**
     * 渲染秒杀订单确认页面相关所有数据
     *
     * @param skuId
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String seckillTradeHtml(Model model, @RequestParam("skuId") Long skuId) {
        Result<Map> mapResult = activityFeignClient.seckillOrderTrade(skuId);
        model.addAllAttributes(mapResult.getData());
        return "/seckill/trade";
    }
}
